package day_8_19;

/**
 * 1423. 可获得的最大点数
 * https://leetcode.cn/problems/maximum-points-you-can-obtain-from-cards/description/
 */
public class Main2 {
    public int maxScore(int[] cardPoints, int k) {
        // 反向思维，计算开头和结尾太难
        // 只需要计算 n-k 的长度下最小的点数，再用总共的点数减去最小的点数即可
        int n = cardPoints.length;
        int m = n - k;
        int s = 0;
        for(int i = 0;i < m;i++) {
            s += cardPoints[i];
        }

        int sum = s;
        int minSum = s;
        for(int i = m;i < n;i++) {
            sum += cardPoints[i];
            // 出窗口和进窗口同时进行
            s += cardPoints[i] - cardPoints[i - m];
            minSum = Math.min(minSum,s);
        }
        return sum - minSum;
    }
}
